<?php

namespace App\Http\Controllers\Caiwu;

use App\Http\Controllers\Controller;
use App\Models\Invoice;
use App\Models\Order;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\View;

class InvoiceController extends Controller
{

    /**
     * 发票列表
     * @param Request $request
     * @return \Illuminate\Contracts\View\View|\Illuminate\Http\JsonResponse
     */
    public function index(Request $request)
    {
        if ($request->ajax()){
            $user = $request->user();
            $res = Invoice::query()
                ->where(function ($q) use($user){
                    if($user->hasPermissionTo('caiwu.invoice.list_all')) {

                    } else {
                        return $q->where('user_id',$user->id);
                    }
                })
                ->orderBy('id','desc')
                ->paginate($request->input('limit',30));
            return $this->success('ok',$res->items(),$res->total());
        }
        return View::make('caiwu.invoice.index');
    }

    public function choseOrder()
    {
        return View::make('caiwu.invoice.chose_order');
    }

    public function create(Request $request)
    {
        $orderId = $request->input('order_id');
        $order = Order::query()->find($orderId);
        return View::make('caiwu.invoice.create',compact('order'));
    }

    public function store(Request $request)
    {
        $data = $request->all([
            'type',
            'money',
            'name',
            'num',
        ]);
        $data = array_merge($data,[
            'user_id' => $request->user()->id,
            'user_nickname' => $request->user()->nickname,
            'order_id' => $request->input('type_bind_id'),
        ]);
        try {
            Invoice::create($data);
            return $this->success();
        }catch (\Exception $exception){
            Log::error('申请发票异常：'.$exception->getMessage());
            return $this->error();
        }
    }

    /**
     * 审核
     * @param Request $request
     * @return \Illuminate\Contracts\View\View|\Illuminate\Http\JsonResponse
     */
    public function check(Request $request)
    {
        $model = Invoice::query()->find($request->input('id'));
        if ($request->ajax()){
            try {
                $data = $request->all(['check_status','check_result','multiple_file']);
                if ($data['check_status']==null){
                    return $this->error('请选择状态');
                }
                $data = array_merge($data,[
                    'check_time' => date('Y-m-d H:i:s'),
                    'check_user_id' => $request->user()->id,
                    'check_user_nickname' => $request->user()->nickname,
                ]);
                $model->update($data);
                return $this->success();
            }catch (\Exception $exception){
                Log::error('审核异常：'.$exception->getMessage());
                return $this->error();
            }
        }
        return View::make('caiwu.invoice.check',compact('model'));
    }

    public function destroy(Request $request)
    {
        $ids = $request->input('ids');
        try {
            Invoice::query()->whereIn('id', $ids)->delete();
            return $this->success();
        } catch (\Exception $exception) {
            Log::error('删除异常：' . $exception->getMessage());
            return $this->error();
        }
    }

}
